#include "MyListener.h"
MyListener::MyListener(RenderWindow* win, Camera* cam, SceneManager *_mSceneMgr) : ExampleFrameListener(win, cam)
{
	mSceneMgr = _mSceneMgr;
	//Entity* head = mSceneMgr->createEntity("Head", "facial.mesh");
	//headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
	//headNode->attachObject(head);
	
	SBObject* theHead = new SBObject("Head","facial.mesh",mSceneMgr);

	luciole = mSceneMgr->createLight("uneLuciole");

	rotationPoint = mSceneMgr->getRootSceneNode()->createChildSceneNode();
	BillboardSet * bSet= mSceneMgr->createBillboardSet("FlareSet");
	bSet->setMaterialName("Sun/Flare");
	Ogre::Billboard * sauronEye = bSet->createBillboard(Vector3(0,0,0));
	lightNode = rotationPoint->createChildSceneNode();
	lightNode->setPosition(-20, 50, 150);
	lightNode->attachObject(luciole);
	lightNode->attachObject(bSet);

	mCamera->setPosition(-20, 50, 150);
	mCamera->lookAt(headNode->getPosition());
}

bool MyListener::frameStarted(const FrameEvent& evt)
{
	bool standardReturn = ExampleFrameListener::frameStarted(evt);
	mCamera->lookAt(headNode->getPosition());
	rotationPoint->yaw(Radian(0.001));
	return standardReturn;      
}

bool MyListener::frameEnded(const FrameEvent& evt)
{
   bool standardReturn = ExampleFrameListener::frameEnded(evt);
   return standardReturn;        
}

bool MyListener::processUnbufferedKeyInput(const FrameEvent& evt)
{
	using namespace OIS;

	if(mKeyboard->isKeyDown(KC_A))
		mTranslateVector.x = -mMoveScale;	// Move camera left

	if(mKeyboard->isKeyDown(KC_D))
		mTranslateVector.x = mMoveScale;	// Move camera RIGHT

	if(mKeyboard->isKeyDown(KC_UP) || mKeyboard->isKeyDown(KC_W) )
		mTranslateVector.z = -mMoveScale;	// Move camera forward

	if(mKeyboard->isKeyDown(KC_DOWN) || mKeyboard->isKeyDown(KC_S) )
		mTranslateVector.z = mMoveScale;	// Move camera backward

	if(mKeyboard->isKeyDown(KC_PGUP))
		mTranslateVector.y = mMoveScale;	// Move camera up

	if(mKeyboard->isKeyDown(KC_PGDOWN))
		mTranslateVector.y = -mMoveScale;	// Move camera down

	if(mKeyboard->isKeyDown(KC_RIGHT))
		mCamera->yaw(-mRotScale);

	if(mKeyboard->isKeyDown(KC_LEFT))
		mCamera->yaw(mRotScale);

	if( mKeyboard->isKeyDown(KC_ESCAPE) || mKeyboard->isKeyDown(KC_Q) )
		return false;

   	if( mKeyboard->isKeyDown(KC_F) && mTimeUntilNextToggle <= 0 )
	{
		mStatsOn = !mStatsOn;
		showDebugOverlay(mStatsOn);
		mTimeUntilNextToggle = 1;
	}

	if( mKeyboard->isKeyDown(KC_T) && mTimeUntilNextToggle <= 0 )
	{
		switch(mFiltering)
		{
		case TFO_BILINEAR:
			mFiltering = TFO_TRILINEAR;
			mAniso = 1;
			break;
		case TFO_TRILINEAR:
			mFiltering = TFO_ANISOTROPIC;
			mAniso = 8;
			break;
		case TFO_ANISOTROPIC:
			mFiltering = TFO_BILINEAR;
			mAniso = 1;
			break;
		default: break;
		}
		MaterialManager::getSingleton().setDefaultTextureFiltering(mFiltering);
		MaterialManager::getSingleton().setDefaultAnisotropy(mAniso);

		showDebugOverlay(mStatsOn);
		mTimeUntilNextToggle = 1;
	}

	if(mKeyboard->isKeyDown(KC_SYSRQ) && mTimeUntilNextToggle <= 0)
	{
		std::ostringstream ss;
		ss << "screenshot_" << ++mNumScreenShots << ".png";
		mWindow->writeContentsToFile(ss.str());
		mTimeUntilNextToggle = 0.5;
		mDebugText = "Saved: " + ss.str();
	}

	if(mKeyboard->isKeyDown(KC_R) && mTimeUntilNextToggle <=0)
	{
		mSceneDetailIndex = (mSceneDetailIndex+1)%3 ;
		switch(mSceneDetailIndex) {
			case 0 : mCamera->setPolygonMode(PM_SOLID); break;
			case 1 : mCamera->setPolygonMode(PM_WIREFRAME); break;
			case 2 : mCamera->setPolygonMode(PM_POINTS); break;
		}
		mTimeUntilNextToggle = 0.5;
	}

	static bool displayCameraDetails = false;
	if(mKeyboard->isKeyDown(KC_P) && mTimeUntilNextToggle <= 0)
	{
		displayCameraDetails = !displayCameraDetails;
		mTimeUntilNextToggle = 0.5;
		if (!displayCameraDetails)
			mDebugText = "";
	}

	// Print camera details
	if(displayCameraDetails)
		mDebugText = "P: " + StringConverter::toString(mCamera->getDerivedPosition()) +
					 " " + "O: " + StringConverter::toString(mCamera->getDerivedOrientation());

	// Return true to continue rendering
	return true;
}

bool MyListener::processUnbufferedMouseInput(const FrameEvent& evt)
{
	using namespace OIS;

	// Rotation factors, may not be used if the second mouse button is pressed
	// 2nd mouse button - slide, otherwise rotate
	const MouseState &ms = mMouse->getMouseState();
	if( ms.buttonDown( MB_Right ) )
	{
		mTranslateVector.x += ms.X.rel * 0.13;
		mTranslateVector.y -= ms.Y.rel * 0.13;
	}
	else
	{
		mRotX = Degree(-ms.X.rel * 0.13);
		mRotY = Degree(-ms.Y.rel * 0.13);
	}

	return true;
}